using System;
using System.Collections.Generic;
using System.Text;
using Microsoft.Practices.CompositeUI;
using Microsoft.Practices.ObjectBuilder;
using SCSFContrib.CompositeUI.WPF.Activation;

namespace SCSFContrib.CompositeUI.WPF.BuilderStrategies
{
	/// <summary>
	/// Implements a <see cref="BuilderStrategy"/> that adds services needed by WPF applications into the <see cref="WorkItem"/>.
	/// </summary>
	public class WindowsServiceStrategy : BuilderStrategy
	{
		/// <summary>
		/// Builds up <paramref name="existing"/>, ensuring that it contains necessary services.
		/// </summary>
		/// <param name="context">
		/// A context for the operation.
		/// </param>
		/// <param name="typeToBuild">
		/// The type of object that is being requested.
		/// </param>
		/// <param name="existing">
		/// The existing instance, if one was passed in, or if a previous strategy has already created the object.
		/// </param>
		/// <param name="idToBuild">
		/// The ID of the object that is being requested. May be <see langword="null"/> if the user requested an unnamed instance.
		/// </param>
		/// <returns>
		/// The built object.
		/// </returns>
		public override object BuildUp(IBuilderContext context, Type typeToBuild, object existing, string idToBuild)
		{
			WorkItem workItem = existing as WorkItem;

			if ((workItem != null) && (!workItem.Services.ContainsLocal(typeof(IFrameworkElementActivationService))))
			{
				workItem.Services.AddNew<FrameworkElementActivationService, IFrameworkElementActivationService>();
			}

			return base.BuildUp(context, typeToBuild, existing, idToBuild);
		}
	}
}
